﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace Listview
{
    public partial class Form1 : Form
    {
        private MySqlConnection myconn = new MySqlConnection("Host = localhost; Database = test; Username = root; Password = root");
            
        public Form1()
        {
            InitializeComponent();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Get("select * from for_test");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listView1.CheckedItems)
            {
                string sql = "update for_test set pwd=12345678 where id=" + item.SubItems[1].Text;
                Change(sql);
               // MessageBox.Show(item.SubItems[1].Text, @"获取");
            }
            Get("select * from for_test");
        }

        private void Get(String sql)
        {
            listView1.Items.Clear();
            listView1.BeginUpdate();
            myconn.Open();
            //string sql = string.Format("select * from for_test");
            MySqlCommand mycom = myconn.CreateCommand();
            mycom.CommandText = sql;
            mycom.CommandType = CommandType.Text;
            MySqlDataReader sdr = mycom.ExecuteReader();
            int i = 0;
            while (sdr.Read())
            {
                listView1.Items.Add("");
                listView1.Items[i].SubItems.Add(sdr["id"].ToString());
                listView1.Items[i].SubItems.Add(sdr["number"].ToString());
                listView1.Items[i].SubItems.Add(sdr["pwd"].ToString());
                ++i;
            }
            myconn.Close();
            listView1.EndUpdate();
        }
        private void Change(string sql)
        {
            myconn.Open();
            MySqlCommand cmd=new MySqlCommand(sql,myconn);
            cmd.ExecuteNonQuery();
            myconn.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
